package com.jiawa.train.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiawa.train.business.domain.TrainCarriage;
import com.jiawa.train.business.domain.TrainSeat;
import com.jiawa.train.business.domain.TrainSeatExample;
import com.jiawa.train.business.enums.SeatColEnum;
import com.jiawa.train.business.mapper.TrainMapper;
import com.jiawa.train.business.mapper.TrainSeatMapper;
import com.jiawa.train.business.req.TrainSeatQueryReq;
import com.jiawa.train.business.req.TrainSeatSaveReq;
import com.jiawa.train.common.resp.PageResult;
import com.jiawa.train.common.util.SnowUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class TrainSeatService {
    //reource是使用名称注入的方式而autowired是使用类型注入的方式
    @Resource
    private TrainSeatMapper trainSeatMapper;

    @Resource
    private TrainMapper trainMapper;

    @Resource
    private TrainCarriageService trainCarriageService;

    //新增或者更新用户信息，通过判断是否有id决定进行新增操作还是更新操作
    public void save(TrainSeatSaveReq req) {
        //将传递的参数赋给TrainSeat对象,在req中没有id,createTime,updateTime的数据，需要自己进行赋值
        //这里的id使用雪花算法，然后创建时间使用当前时间
        DateTime now = DateTime.now();
        TrainSeat trainSeat = BeanUtil.copyProperties(req, TrainSeat.class);
        //如果id为空，则进行新增操作
        if(ObjectUtil.isNull(trainSeat.getId())){
            trainSeat.setId(SnowUtil.getSnowflakeNextId());
            trainSeat.setCreateTime(now);
            trainSeat.setUpdateTime(now);
            trainSeatMapper.insert(trainSeat);
        }
        //否则进行更新操作
        else{
            trainSeat.setUpdateTime(now);
            trainSeatMapper.updateByPrimaryKeySelective( trainSeat);
        }
    }

    //如果用户id存在则进行根据id进行查询乘客的信息，如果是用户id不存在则进行查询所有,全部都按分页进行处理
    public PageResult queryTrainSeatList(TrainSeatQueryReq  req) {
        //创建trainSeatExample对象
        TrainSeatExample trainSeatExample = new TrainSeatExample();
        trainSeatExample.setOrderByClause("train_code asc,carriage_index asc, carriage_seat_index asc");
        //设置查询条件
        TrainSeatExample.Criteria criteria = trainSeatExample.createCriteria();
        if(req.getTrainCode() != "" && req.getTrainCode() != null){
            criteria.andTrainCodeEqualTo(req.getTrainCode());
        }
        // 先设置分页的参数
        PageHelper.startPage(req.getPage(),req.getPageSize());
        //进行查询数据库
        List<TrainSeat> trainSeatList = trainSeatMapper.selectByExample(trainSeatExample);
        //返回查询结果,转为分页的形式,
        PageInfo<TrainSeat> pageInfo = new PageInfo<>(trainSeatList);
        //获取数据进行封装
        PageResult pageResult = new PageResult(pageInfo.getTotal(), pageInfo.getList());
        //返回查询结果
        return pageResult;
    }

    public void delete(Long id) {
        trainSeatMapper.deleteByPrimaryKey(id);
    }

    //根据火车车次信息和车厢信息生成座位信息
    public void genSeat(String trainCode) {
        DateTime now = DateTime.now();
        //第一步：先根据火车车次查询所有的座位信息，将其进行删除
        TrainSeatExample trainSeatExample = new TrainSeatExample();
        trainSeatExample.createCriteria().andTrainCodeEqualTo(trainCode);
        trainSeatMapper.deleteByExample(trainSeatExample);
        //第二步：根据火车车次查询火车车厢的信息进行返回，得到车厢信息
        List<TrainCarriage> trainCarriageList = trainCarriageService.queryListByTrainCode(trainCode);
        log.info("当前车次下的车厢数：{}", trainCarriageList.size());
        //第三步：循环生成每个车厢的座位，遍历车厢
       for(TrainCarriage trainCarriage : trainCarriageList){
           //第四步：获取车厢的排数和座位类型
           Integer rowCount = trainCarriage.getRowCount();
           String seatType = trainCarriage.getSeatType();
           int seatIndex = 1;

           //第五步：根据座位类型获取座位列数,比如说是一等座，筛选出的为{ACDF}
           List<SeatColEnum> colEnumList = SeatColEnum.getColsByType(seatType);
           log.info("根据车厢的座位类型，筛选出所有的列：{}", colEnumList);

           //第六步：循环遍历车厢的排数
           for(int row = 1; row <= rowCount; row++){
               //第七步：循环遍历车厢的列数
               for (SeatColEnum seatColEnum : colEnumList) {
                   //第八步：生成座位信息
                   TrainSeat trainSeat = new TrainSeat();
                   trainSeat.setId(SnowUtil.getSnowflakeNextId());
                   trainSeat.setTrainCode(trainCode);
                   trainSeat.setCarriageIndex(trainCarriage.getIndex());
                   trainSeat.setRow(StrUtil.fillBefore(String.valueOf(row), '0', 2));
                   trainSeat.setCol(seatColEnum.getCode());
                   trainSeat.setSeatType(seatType);
                   trainSeat.setCarriageSeatIndex(seatIndex++);
                   trainSeat.setCreateTime(now);
                   trainSeat.setUpdateTime(now);
                   trainSeatMapper.insert(trainSeat);
               }
           }
        }
    }

    public List<TrainSeat> selectByTrainCode(String trainCode) {
        TrainSeatExample trainSeatExample = new TrainSeatExample();
        trainSeatExample.createCriteria().andTrainCodeEqualTo(trainCode);
        return trainSeatMapper.selectByExample(trainSeatExample);
    }
}